package com.example.myapplication.storage;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication.R;
import com.example.myapplication.application.CustomApplication;
import com.example.myapplication.storage.dao.BookDao;
import com.example.myapplication.storage.entity.Book;
import com.example.myapplication.utils.ToastUtils;
import com.example.myapplication.widget.EditTextActivity;

import java.util.List;

/**
 * 演示Room操作SQLite数据库
 */
public class RoomStorageUsageActivity extends AppCompatActivity implements View.OnClickListener {

    private BookDao bookDao;

    private EditText etName;
    private EditText etAuthor;
    private EditText etPress;
    private EditText etPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_room_storage_usage);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        etName = findViewById(R.id.et_name);
        etAuthor = findViewById(R.id.et_author);
        etPress = findViewById(R.id.et_press);
        etPrice = findViewById(R.id.et_price);

        findViewById(R.id.btn_add).setOnClickListener(this);
        findViewById(R.id.btn_delete).setOnClickListener(this);
        findViewById(R.id.btn_update).setOnClickListener(this);
        findViewById(R.id.btn_query).setOnClickListener(this);

        // 获取book表的dao实例
        bookDao = CustomApplication.getInstance().getRoomUsageDatabase().bookDao();
    }

    @Override
    public void onClick(View v) {
        Book book = buildBookFromEdit();
        if (v.getId() == R.id.btn_add) {
            bookDao.insert(book);
            ToastUtils.show(this, "添加书本成功！");
//            if (insertRows < 1) {
//                ToastUtils.show(this, "添加书本信息失败！");
//            }
        } else if (v.getId() == R.id.btn_delete) {

        } else if (v.getId() == R.id.btn_update) {

        } else if (v.getId() == R.id.btn_query) {
            List<Book> books = bookDao.queryAll();
            for (Book b : books) {
                Log.d("RoomStorageUsage", b.toString());
            }
        }
    }

    public Book buildBookFromEdit() {
        Book book = new Book();
        book.setName(etName.getText().toString());
        book.setAuthor(etAuthor.getText().toString());
        book.setPress(etPress.getText().toString());
        try {
            String priceText = etPrice.getText().toString();
            if (!priceText.trim().isEmpty()) {
                book.setPrice(Double.valueOf(priceText));
            }
        } catch (NumberFormatException e) {
            Log.e("RoomStorageUsage", "价格的值格式错误，不是有效的数值", e);
        }
        return book;
    }
}